﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calligraphy.CalligraphyData
{
    partial class T_StelePaper
    {
                
        public string SubTitle
        {
            get
            {
                string subTitle = string.Empty;
                if (T_Dynast!= null)
                    subTitle += T_Dynast.Name.Trim() + "\t";
                if(T_Style != null)
                    subTitle += T_Style.Name.Trim();

                return subTitle;
            }
        }


        public string CustomDescription
        {
            get
            {
                string str = string.Empty;
                str = AddTimeDescripiton(str);
                str = AddSizeDescription(str);
                str = AddLocationDescription(str);
                str = AddSubDescription(str);
                return str;
            }
        }

        private string AddSubDescription(string str)
        {
            if (!string.IsNullOrEmpty(SubDescription))
            {
                str = AddNewLine(str);
                str += SubDescription.Substring(0, 100);
            }
            return str;
        }

        private  string AddLocationDescription(string str)
        {
            if (!string.IsNullOrEmpty(Loacation))
            {
                str = AddNewLine(str);
                str += "地点：";
                str += "\t";
                str += Loacation;
            }
            return str;
        }

        private string AddSizeDescription(string str)
        {
            if (Height.HasValue && Width.HasValue)
            {
                str = AddTab(str);
                str += "尺寸（米）：";
                str += "高";
                str += Height.Value.ToString("0.##");
                str += " 宽";
                str += Width.Value.ToString("0.##");
            }
            return str;
        }

        private string AddTimeDescripiton(string str)
        {
            if (Year.HasValue || T_Dynast != null)
            {
                str += "时间：";
                str += "\t";
                if (T_Dynast != null)
                {
                    str += T_Dynast.Name;
                    str += " ";
                }
                if (Year.HasValue)
                {
                    str += Year.Value.ToString("yyyy");
                }
            }
            return str;
        }

        private static string AddTab(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str += "\t";
            }
            return str;
        }

        private static string AddNewLine(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str += "\r\n";
            }
            return str;
        }

        Uri _photo250;
        public Uri Photo250
        {
            get
            {
                if (_photo250 == null)
                {
                    LoadSmallPhotoEntities();
                }
                return _photo250;
            }
        }

        Uri _photo400;
        public Uri Photo400
        {
            get
            {
                if (_photo400 == null)
                {
                    LoadSmallPhotoEntities();
                }
                return _photo400;
            }
        }

        

        private void LoadSmallPhotoEntities()
        {
            if (T_Photo == null)
                return;
            if (T_Photo.T_Photo1.Count > 0)
                return;
            var q = from sp in DataServiceContextHelper.DataContext.T_Photo where sp.RawPhotoID == T_Photo.ID && (sp.IsDeleted == null || sp.IsDeleted == false) select sp;
            T_Photo.T_Photo1.LoadAsync(q);
            T_Photo.T_Photo1.LoadCompleted += T_Photo1_LoadCompleted;
        }



        void T_Photo1_LoadCompleted(object sender, System.Data.Services.Client.LoadCompletedEventArgs e)
        {
            LoadPhoto250Uri();
            LoadPhoto400Uri();
        }

        private async void LoadPhoto250Uri()
        {
            T_Photo smallphotoEntity = PhotoLoader.GetSmallPhoto(T_Photo, PhotoLoader.SmallPhotoSizes250);
            if (smallphotoEntity != null)
            {
                _photo250 =
                    await PhotoLoader.GetImageUriAsync(smallphotoEntity);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,new System.ComponentModel.PropertyChangedEventArgs("Photo250"));
                }
            }
        }
        private async void LoadPhoto400Uri()
        {
            T_Photo smallphotoEntity = PhotoLoader.GetSmallPhoto(T_Photo, PhotoLoader.SmallPhotoSizes400);
            if (smallphotoEntity != null)
            {
                _photo400 =
                    await PhotoLoader.GetImageUriAsync(smallphotoEntity);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Photo400"));
                }
            }
        }
    }
}
